//
//  PositionViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "PositionViewController.h"
#import "WokerViewController.h"

#import "CompanyCell.h"
#import "HBHMainResumeCell.h"
@interface PositionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@end

@implementation PositionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    [self getDeliveryRecordList];
}

-(void)creatView
{
    [self initTitleWithTitle:@"我投递的职位"];
    _pageNumber = 1;
    _dataArr = [NSMutableArray new];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
//        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _pageNumber = 1;
            [_dataArr removeAllObjects];
            [self getDeliveryRecordList];
        }];
        //加载
        _tableV.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            _pageNumber++;
            [self getDeliveryRecordList];
        }];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 0) {
        return 108*screenW_6;
//    }
//    return 70*screenW_6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 0) {
        HBHMainResumeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHMainResumeCell"];
        if (!cell) {
            cell = [[HBHMainResumeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHMainResumeCell"];
        }
        [cell loadCellWithData:_dataArr[indexPath.row]];
        return cell;
//    }else{
//        CompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CompanyCell"];
//        if (!cell) {
//            cell = [[CompanyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CompanyCell"];
//        }
//        [cell setDataDic:nil];
//        return cell;
//    }
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteCell:indexPath];//删除
}

-(void)deleteCell:(NSIndexPath *)index//删除事件
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/deleteDeliveryRecord" Dic:@{@"id":_dataArr[index.row][@"id"]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [_dataArr removeObjectAtIndex:index.row];
                    [_tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationTop];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section > _dataArr.count) {
        return;
    }
    WokerViewController *vc = [WokerViewController new];
    vc.wordId = _dataArr[indexPath.row][@"positionId"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getDeliveryRecordList
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/deliveryRecordList" Dic:@{@"userId":[Storage getToken],@"pageNo":[NSString stringWithFormat:@"%ld",_pageNumber],@"pageSize":@"10"} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    if ([responseDic[@"count"] integerValue] > _dataArr.count) {
                        [_dataArr addObjectsFromArray:responseDic[@"data"]];
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [_tableV reloadData];
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    if ([responseDic[@"count"] integerValue] <= _dataArr.count) {
                        [_tableV.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        [_tableV.mj_footer endRefreshing];
                    }
                }
            });
        }];
    });
}

@end
